package com.voute.View.Adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.voute.App.App;
import com.voute.Model.Bean.DiscoveryFeed;
import com.voute.R;
import com.voute.Utils.DisplayUtil;
import com.voute.Utils.GlideUtils;
import com.voute.Utils.ViewHolderUtils;
import com.voute.View.Activity.CircleActivity;
import com.voute.View.Activity.FeedDetailInfoActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 发现adapter
 * Created by liukanshan on 2016/12/9.
 */

public class DiscoveryAdapter extends BaseAdapter {

    private ArrayList<DiscoveryFeed> data = new ArrayList<>();
    private Context mContext;

    public DiscoveryAdapter(Context context, ArrayList<DiscoveryFeed> datas) {
        this.mContext = context;
        if (datas != null) {
            data.clear();
            data.addAll(datas);
        }
    }

    public void setData(List<DiscoveryFeed> datas) {
        if (datas != null) {
            data.clear();
            data.addAll(datas);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.adapter_discovery_framelayout, null);
        }
        initViews(position, convertView);
        return convertView;
    }

    private void initViews(final int position, View convertView) {
        final DiscoveryFeed feed=data.get(position);
        ImageView adapter_discovery_imager= ViewHolderUtils.get(convertView,R.id.adapter_discovery_imager);
        TextView adapter_discovery_time=ViewHolderUtils.get(convertView, R.id.adapter_discovery_time);
        TextView adapter_discovery_vote_num=ViewHolderUtils.get(convertView,R.id.adapter_discovery_vote_num);
        TextView adapter_discovery_desc=ViewHolderUtils.get(convertView,R.id.adapter_discovery_desc);
        adapter_discovery_desc.setText(feed.getTitle());
        TextView adapter_tv_circle=ViewHolderUtils.get(convertView,R.id.adapter_tv_circle);
        if(!TextUtils.isEmpty(feed.getCircle_name())){
            adapter_tv_circle.setVisibility(View.VISIBLE);
            adapter_tv_circle.setText(feed.getCircle_name());
            Drawable rightDrawable = mContext.getResources().getDrawable(R.mipmap.adapter_feed_time);
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
            adapter_discovery_time.setCompoundDrawables(rightDrawable,null, null, null);
            adapter_discovery_time.setVisibility(View.VISIBLE);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(mContext, FeedDetailInfoActivity.class);
                    intent.putExtra("KEY_FEED_HASH",feed.getHash());
                    mContext.startActivity(intent);
                }
            });
            adapter_tv_circle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(mContext, CircleActivity.class);
                    intent.putExtra("KEY_CIRCLE_TITLE",feed.getTitle());
                    intent.putExtra("KEY_CIRCLE_HASH",feed.getCircle_hash());
                    mContext.startActivity(intent);
                }
            });
            adapter_discovery_time.setText(feed.getLive_time());
        }
        else{
            adapter_tv_circle.setVisibility(View.INVISIBLE);
            Drawable rightDrawable = mContext.getResources().getDrawable(R.mipmap.adapter_nearby);
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
            adapter_discovery_time.setCompoundDrawables(rightDrawable,null, null, null);
            if(feed.isNearby()){
                adapter_discovery_time.setVisibility(View.VISIBLE);
                adapter_discovery_time.setText("附近");
            }
            else {
                adapter_discovery_time.setVisibility(View.INVISIBLE);
            }
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(mContext, FeedDetailInfoActivity.class);
                    intent.putExtra("KEY_FEED_HASH",feed.getHash());
                    mContext.startActivity(intent);
                }
            });
        }
        adapter_discovery_vote_num.setText(feed.getAll_vote()+"");
        if(!TextUtils.isEmpty(feed.getPic())){
            adapter_discovery_imager.setVisibility(View.VISIBLE);
            adapter_discovery_imager.setMaxHeight(9* (DisplayUtil.GetDisplayWidth()-DisplayUtil.dip2px(App.GetInstance(),24))/16);
            GlideUtils.getInstance().LoadContextBitmap(mContext,feed.getPic(),adapter_discovery_imager,R.mipmap.item_backgroud,R.mipmap.item_backgroud,GlideUtils.LOAD_BITMAP);
        }
        else{
            adapter_discovery_imager.setVisibility(View.GONE);
        }
    }
}
